package com.xuexi.controller;

import com.easygo.utils.MessageResults;
import com.easygo.utils.PageUtils;
import com.xuexi.api.SpecificationClient;
import com.xuexi.easygo.pojo.Specification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.Map;

@Controller
@Scope("prototype")
public class SpecificationController {

    @Autowired
    SpecificationClient specificationClient;

    @RequestMapping("specification_add")
    @ResponseBody
    public MessageResults addSpecification(@RequestParam(value = "spec_name") String spec_name,
                                           @RequestParam(value = "option_name")String[] option_name,
                                           @RequestParam(value = "orders")Integer[] orders){

        //两个表中的值
//        System.out.println(spec_name);
//        System.out.println(Arrays.toString(option_name));
//        System.out.println(Arrays.toString(orders));
        MessageResults results= null;
        try {
            Integer count = specificationClient.specification_add(spec_name, option_name, orders);
            if (count > 0) {
                results = new MessageResults(200, "新增成功");
            } else {
                results = new MessageResults(500, "新增失败");
            }
        } catch (Exception e) {
            results = new MessageResults(503,"服务器异常");
            e.printStackTrace();
        }
        return results;
    }

    @RequestMapping("specification_page")
    public String specification_page(@RequestParam(defaultValue = "1", required = false,value = "pageIndex") Integer pageIndex,
                                     @RequestParam(defaultValue = "5", required = false,value = "pageSize") Integer pageSize,
                                     @RequestParam(defaultValue = "", required = false,value = "spec_name") String spec_name, Model model){
        PageUtils<Specification> pageUtils = specificationClient.specification_pages(pageIndex, pageSize, spec_name);
//        System.out.println(pageUtils);
        model.addAttribute("pageUtils",pageUtils);
        //回显条件
        model.addAttribute("spec_name",spec_name);
        return "specification";
    }

    //更新的时候数据回显
    @RequestMapping("specification_getById")
    @ResponseBody
    public Map<String,Object> getSpecification(@RequestParam(value = "id") Integer id){
        return specificationClient.getSpecification(id);
    }

    @RequestMapping("specification_update")
    @ResponseBody
    public MessageResults specification_update(@RequestParam(value = "id") Integer id,
                                               @RequestParam(value = "spec_name") String spec_name,
                                               @RequestParam(value = "option_name") String[] option_name,
                                               @RequestParam(value = "orders") Integer[] orders){
        //两个表中的值
        System.out.println(id);
        System.out.println(spec_name);
        System.out.println(Arrays.toString(option_name));
        System.out.println(Arrays.toString(orders));
        MessageResults results = null;
        try {
            Integer count = specificationClient.specification_update(id,spec_name, option_name, orders);
            if (count > 0) {
                results = new MessageResults(200, "更新成功");
            } else {
                results = new MessageResults(500, "更新失败");
            }
        } catch (Exception e) {
            results = new MessageResults(503, "服务器异常...");
            e.printStackTrace();
        }
        return results;
    }
}
